<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of webservice
 *
 * @author pudy
 */
class Server extends CI_Controller {

    //put your code here
    
    function getAllCategories() {
        $this->load->model('categoryModel', 'categoryModel');
        $getAllCategories = $this->categoryModel->getCategories();
        
        $output = json_encode($getAllCategories);
        print($output);
    }
    
    function loginMobile() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');

        $this->load->model('userModel', 'userModel');

        $options = array('username' => $username, 'password' => $password);
        $getReturn = $this->userModel->getUsers($options);

        if (!$getReturn) {
            echo 0;
        } else {
            $id = $getReturn[0]->id;
            echo $id;
        }
    }

    function getUserPoi() {
        $user_id = $this->input->post('user_id');
        //$user_id = 3;

        $this->load->model('poiModel', 'poiModel');

        $options = array(
            'user_id' => $user_id,
            'columnSelect' => 'id, title'
        );
        $getReturnPois = $this->poiModel->getPois($options);

        $output = json_encode($getReturnPois);
        print($output);
    }

    function updatePoiLocation() {
        $user_id = $this->input->post('user_id');
        $poi_title = $this->input->post('poi_title');
        $latitude = $this->input->post('latitude');
        $longitude = $this->input->post('longitude');

        //echo $user_id.$poi_title.$latitude.$longitude;
        //$user_id = 1;
        //$poi_title = 'Holiday Inn Bandung';
        //$latitude = $this->input->post('latitude');
        //$longitude = $this->input->post('longitude');

        $this->load->model('poiModel', 'poiModel');

        $options = array(
            'user_id' => $user_id,
            'title' => $poi_title,
            'columnSelect' => 'id'
        );
        $getReturnPois = $this->poiModel->getPois($options);

        if (!$getReturnPois) {
            echo -1;
        } else {
            $poi_id = $getReturnPois[0]->id;
            $options2 = array(
                'id' => $poi_id,
                'latitude' => $latitude,
                'longitude' => $longitude
            );

            $getReturnUpdate = $this->poiModel->updatePoi($options2);

            if (!$getReturnUpdate) {
                echo 0;
            } else {
                echo 1;
            }
        }
    }

    function getNearbyPois() {
        $category = $this->input->post('category');
        $distance = $this->input->post('distance');
        $latitude = $this->input->post('latitude');
        $longitude = $this->input->post('longitude');
         /*$category = 'restaurant';
          $distance = '2000';
          $latitude = '-6.894804';
          $longitude = '107.612983';*/
        $this->load->model('poiModel', 'poiModel');
        if ($category == 'all') {
            $options = array(
                'columnSelect' => 'id, title, description, latitude, longitude'
            );
            $getReturnPois = $this->poiModel->getPois($options);
        } else {
            //get category_id
            $this->load->model('categoryModel', 'categoryModel');
            $options = array(
                'name' => $category,
                'columnSelect' => 'id'
            );
            $getCategoryId = $this->categoryModel->getCategories($options);
            if (is_bool($getCategoryId)) {
                //error : category_id tidak ditemukan
            } else {
                $category_id = $getCategoryId[0]->id;
                
                $options = array(
                    'category_id' => $category_id,
                    'columnSelect' => 'id, title, description, latitude, longitude'
                );
                $getReturnPois = $this->poiModel->getPois($options);
            }
        }

        if (is_bool($getReturnPois)) {
            //tidak ada POI yang memenuhi kriteria
            print(json_encode(null));
        } else {
            //eliminasi berdasarkan jarak terhadap lokasi device
            $nearbypoi = array();
            foreach ($getReturnPois as $poi) {
                $lat1 = $poi->latitude;
                $lon1 = $poi->longitude;
                $lat2 = doubleval($latitude);
                $lon2 = doubleval($longitude);

                $dLat = deg2rad($lat1 - $lat2);
                $dLon = deg2rad($lon1 - $lon2);

                //Haversine Formula
                //$a = (sin($dLat / 2) * sin($dLat / 2)) + (sin($dLon / 2) * sin($dLon / 2) * cos($lat1) * cos($lat2));
                //$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
                //$d = 6371000 * $c;
                //end of Haversine Formula
                
                //Spherical Law of Cosines
                $d = acos(sin(deg2rad($lat1))*sin(deg2rad($lat2)) + cos(deg2rad($lat1))*cos(deg2rad($lat2))*cos(deg2rad($dLon))) * 6371000;
                //end of Spherical Law of Cosines

                if ($d <= floatval($distance)) {
                    //get objects
                    $this->load->model('objectModel', 'objectModel');
                    $options = array(
                        'poi_id' => $poi->id,
                        'columnSelect' => 'id, path, type'
                    );
                    $getReturnObjects = $this->objectModel->getObjects($options);
                    
                    $tempObject = array();
                    if (is_bool($getReturnObjects)) {
                    } else {
                        foreach ($getReturnObjects as $object) {
                            $objectElmt = array(
                                'object_id' => $object->id,
                                'object_path' => $object->path,
                                'object_type' => $object->type
                            );
                            $tempObject[] = $objectElmt;
                        }
                    }
                    // end of get objects
                    
                    $temp = array(
                        'id' => $poi->id,
                        'title' => $poi->title,
                        'description' => $poi->description,
                        'latitude' => $poi->latitude,
                        'longitude' => $poi->longitude,
                        'distance' => $d,
                        'objects' => $tempObject
                    );
                    $nearbypoi[] = $temp;
                }
            }
            /* foreach ($nearbypoi as $poi) {
              print_r($poi);
              echo br(1);
              } */
            print(json_encode($nearbypoi));
        }
    }

    function addPoi() {
        $user_id = $this->input->post('user_id');
        $title = $this->input->post('title');
        $description = $this->input->post('description');
        $category = $this->input->post('category');
        $latitude = $this->input->post('latitude');
        $longitude = $this->input->post('longitude');

        //get category_id
        $this->load->model('categoryModel', 'categoryModel');
        $options = array(
            'name' => $category
        );
        $getReturnCategories = $this->categoryModel->getCategories($options);
        if (!is_bool($getReturnCategories)) {
            $category_id = $getReturnCategories[0]->id;
        }

        //retrieve all POIs
        $this->load->model('poiModel', 'poiModel');
        $options = array(
            'columnSelect' => 'title, latitude, longitude'
        );
        $getAllPois = $this->poiModel->getPois($options);

        if (is_bool($getAllPois)) {
            //add ke database karena belum ada POI di database
            $options = array(
                'user_id' => $user_id,
                'title' => $title,
                'description' => $description,
                'latitude' => $latitude,
                'longitude' => $longitude,
                'category_id' => $category_id
            );
            $this->load->model('poiModel', 'poiModel');
            $getReturnInsert = $this->poiModel->addPoi($options);
            
            if (is_bool($getReturnInsert)) {
                //gagal add
                echo 0;
            } else {
                //berhasil add
                echo 1;
            }
        } else {
            $suspiciouspoi = array();
            foreach ($getAllPois as $poi) {
                $lat1 = $poi->latitude;
                $lon1 = $poi->longitude;
                $lat2 = doubleval($latitude);
                $lon2 = doubleval($longitude);

                $dLat = deg2rad($lat1 - $lat2);
                $dLon = deg2rad($lon1 - $lon2);

                $a = (sin($dLat / 2) * sin($dLat / 2)) + (sin($dLon / 2) * sin($dLon / 2) * cos($lat1) * cos($lat2));
                $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
                $d = 6371000 * $c;

                if ($d <= 500) {
                    $suspiciouspoi[] = $poi;
                }
            }

            if (count($suspiciouspoi) > 0) {
                //kirim json suspiciouspoi ke client
                print(json_encode($suspiciouspoi));
                //echo 'ada suspicious poi';
            } else {
                //add ke database karena belum ada POI di database yg berada dalam radius 500 meter dari POI masukan
                $options = array(
                    'user_id' => $user_id,
                    'title' => $title,
                    'description' => $description,
                    'latitude' => $latitude,
                    'longitude' => $longitude,
                    'category_id' => $category_id
                );
                $this->load->model('poiModel', 'poiModel');
                $getReturnInsert = $this->poiModel->addPoi($options);
                if (is_bool($getReturnInsert)) {
                    //gagal add
                    echo 0;
                } else {
                    //berhasil add
                    echo 1;
                }
            }
        }
    }
    
    function verifyAddPoi() {
        $user_id = $this->input->post('user_id');
        $title = $this->input->post('title');
        $description = $this->input->post('description');
        $category = $this->input->post('category');
        $latitude = $this->input->post('latitude');
        $longitude = $this->input->post('longitude');

        //get category_id
        $this->load->model('categoryModel', 'categoryModel');
        $options = array(
            'name' => $category
        );
        $getReturnCategories = $this->categoryModel->getCategories($options);
        if (!is_bool($getReturnCategories)) {
            $category_id = $getReturnCategories[0]->id;
        }

        //add POI into database
        $options = array(
            'user_id' => $user_id,
            'title' => $title,
            'description' => $description,
            'latitude' => $latitude,
            'longitude' => $longitude,
            'category_id' => $category_id
        );
        $this->load->model('poiModel', 'poiModel');
        $getReturnInsert = $this->poiModel->addPoi($options);
        if (is_bool($getReturnInsert)) {
            //gagal add
            echo 0;
        } else {
            //berhasil add
            echo 1;
        }
    }

}

?>
